package com.peakmain.wanandroid.projectmodule.presenter;

import android.annotation.SuppressLint;

import com.peakmain.wanandroid.netmodule.base.BasePresenter;
import com.peakmain.wanandroid.netmodule.bean.DataResponse;
import com.peakmain.wanandroid.netmodule.bean.project.ProjectListBean;
import com.peakmain.wanandroid.netmodule.bean.project.ProjectTreeBean;
import com.peakmain.wanandroid.netmodule.net.ApiService;
import com.peakmain.wanandroid.netmodule.net.RetrofitManager;
import com.peakmain.wanandroid.netmodule.net.RxSchedulers;
import com.peakmain.wanandroid.projectmodule.contract.ProjectTreeContract;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * author: peakmain
 * createdata：2019/7/12
 * mail: 2726449200@qq.com
 * desiption:
 */
public class ProjectTreePresenter extends BasePresenter<ProjectTreeContract.View> implements ProjectTreeContract.Presenter {

    @Inject
    public ProjectTreePresenter(){

    }
    @SuppressLint("CheckResult")
    @Override
    public void getProjectTree() {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).getProjectTree()
                .compose(mView.<DataResponse<List<ProjectTreeBean>>>bindToLife())
                .compose(RxSchedulers.<DataResponse<List<ProjectTreeBean>>>applySchedulers())
                .subscribe(new Consumer<DataResponse<List<ProjectTreeBean>>>() {
                    @Override
                    public void accept(DataResponse<List<ProjectTreeBean>> dataResponse) throws Exception {
                        if(dataResponse.getErrorCode()==0){
                           mView.showProjectTreeSuccess(dataResponse.getData());
                        }else{
                            mView.showFaild(dataResponse.getErrorMessage());
                        }
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild("网络错误，请稍后重试");
                        mView.hideLoading();
                    }
                });
    }


}
